#include "stdafx.h"

const double SpawnBullet::rotateVelocity = 10;

SpawnBullet::SpawnBullet(double x, double y, double vx, double vy)
	:AbstractBullet(x, y), vx(vx), vy(vy)
{

}

SpawnBullet::~SpawnBullet()
{
}

bool SpawnBullet::update(double dtime)
{
	time += dtime;

	angle += dtime*rotateVelocity;
	x -= dtime*vx;
	y -= dtime*vy;

	if (time > lifeSpan)
	{
		die();
		return false;
	}
	else if (outofMap())
	{
		return false;
	}
	else if (collide())
	{
		die();
		return false;
	}
	else if (hero->distance(x, y) <= radius + hero->getRadius())
	{
		die();
		return false;
	}

	return true;
}

bool SpawnBullet::render()
{
	return img->render(getDisplayX(), getDisplayY(), angle);
}

void SpawnBullet::die()
{
	spawn->setX(x);
	spawn->setY(y);
	EnemyMgr::getInstance()->addSpawn(spawn);

	FrameAnimation * anim = new FrameAnimation(x, y);
	anim->setCount(6);
	anim->setFolder("image/animation/spawnBullet");
	anim->setInterval(0.02);
	anim->setMap(map);
	AnimationMgr::getInstance()->add(anim);
}

void SpawnBullet::setImage(const std::string & filename)
{
	img = ImageMgr::getInstance()->get(filename);

	radius = std::min(img->getHeight(), img->getWidth());
}